<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('./mixins/macarons') // echarts theme

import resize from './mixins/resize.js'

const animationDuration = 6000

export default {
  name:'Barchart',
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    }
  },
  data() {
    return {
      chart: null,
      q:[],
      w:[],
      e:[],
      e:[],
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.axios({
        method: "GET",
        url: "https://admin.xiaoheihe.work/bapi/selectFlex",
      }).then((response) => {
        this.q = response.data.q
        this.w = response.data.w
        this.e = response.data.e
        this.r = response.data.r
        this.initChart(this.q,this.w,this.e,this.r)
      })
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart(q,w,e,r) {
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        grid: {
          top: 10,
          left: '2%',
          right: '2%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [{
          type: 'category',
          data: ['7天前', '6天前', '5天前', '4天前', '前天', '昨天', '今天'],
          axisTick: {
            alignWithLabel: true
          }
        }],
        yAxis: [{
          type: 'value',
          axisTick: {
            show: false
          }
        }],
        series: [{
          name: '便宜货订单',
          type: 'bar',
          stack: 'vistors',
          barWidth: '60%',
          data: q,
          animationDuration
        }, {
          name: '大众货订单',
          type: 'bar',
          stack: 'vistors',
          barWidth: '60%',
          data: w,
          animationDuration
        }, {
          name: '高级货订单',
          type: 'bar',
          stack: 'vistors',
          barWidth: '60%',
          data: e,
          animationDuration
          }, {
          name: '奢侈品订单',
          type: 'bar',
          stack: 'vistors',
          barWidth: '60%',
          data: r,
          animationDuration
          }
        ]
      })
    }
  }
}
</script>
